package com.sykj.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sykj.springboot.common.Exception.BussinessException;
import com.sykj.springboot.common.entity.DataResponse;
import com.sykj.springboot.common.utils.Md5Utils;
import com.sykj.springboot.entity.SysUser;
import com.sykj.springboot.entity.vo.SysMenuVo;
import com.sykj.springboot.service.SysMenuService;
import com.sykj.springboot.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author derking
 * @since 2022-09-21
 */
@RestController
@RequestMapping("sysUser")
public class SysUserController {

    @Autowired
    SysUserService sysUserService;

    /**
    * @Description:获取用户列表
    * @Author: lining
    * @Date: 14:47 2022/9/22
    */
    @RequestMapping("getUserList")
    public DataResponse getUserList(HttpServletRequest request,SysUser user){
        DataResponse response = new DataResponse();

        QueryWrapper<SysUser> objectQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(user.getId())) {
            objectQueryWrapper.eq("id",user.getId());
        }
        if (!StringUtils.isEmpty(user.getUsername())) {
            objectQueryWrapper.eq("username",user.getUsername());
        }
        if (!StringUtils.isEmpty(user.getAddress())) {
            objectQueryWrapper.eq("address",user.getAddress());
        }
        List<SysUser> list = sysUserService.list(objectQueryWrapper);
        response.setData(list);
        return response;
    }

    /**
    * @Description: 添加用户
    * @Author: lining
    * @Date: 14:45 2022/9/22
    */
    @RequestMapping("addUser")
    public DataResponse addUser(HttpServletRequest request,SysUser user) throws Exception{
        DataResponse response = new DataResponse();
        if (StringUtils.isNotBlank(user.getId())){
            SysUser byId = sysUserService.getById(user.getId());
            if (byId!=null){
                throw new BussinessException("账号已存在");
            }
        }
        user.setPassword(Md5Utils.md5("123"));
        boolean save = sysUserService.save(user);
        if (!save){
            throw new Exception();
        }
        return response;
    }

    /**
    * @Description:删除用户
    * @Author: lining
    * @Date: 14:46 2022/9/22
    */
    @RequestMapping("delUser")
    public DataResponse delUser(HttpServletRequest request,String ids) throws Exception{
        DataResponse response = new DataResponse();
        if (StringUtils.isBlank(ids)){
            throw new BussinessException("请选择数据");
        }
        boolean del = sysUserService.removeByIds(Arrays.asList(ids.split(",")));
        if (!del){
            throw new Exception();
        }
        return response;
    }

    /**
     * @Description:查看用户
     * @Author: lining
     * @Date: 14:46 2022/9/22
     */
    @RequestMapping("quertDetail")
    public DataResponse quertDetail(HttpServletRequest request,String id) throws Exception{
        DataResponse response = new DataResponse();
        if (StringUtils.isBlank(id)){
            throw new BussinessException("请选择数据");
        }
        SysUser byId = sysUserService.getById(id);
        response.setData(byId);
        return response;
    }


}
